package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.PayOrderType;
import net.xdclass.manager.WxPayManager;
import net.xdclass.mapper.WxOrderInfoMapper;
import net.xdclass.mapper.WxOrderStatusMapper;
import net.xdclass.model.WxOrderInfoDO;
import net.xdclass.model.WxOrderStatusDO;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @description
 * @since 2023-07-02 16:30
 */
@Component
@Slf4j
public class WxPayManagerImpl implements WxPayManager {

    @Autowired
    private WxOrderInfoMapper wxOrderInfoMapper;

    @Autowired
    private WxOrderStatusMapper wxOrderStatusMapper;

    @Override
    public Integer insertPrepareOrder(WxOrderInfoDO wxOrderInfoDO) {
        int orderInfoRows = wxOrderInfoMapper.insert(wxOrderInfoDO);
        log.info("插入商户支付信息: {} 条", orderInfoRows);
        return orderInfoRows;
    }

    @Override
    public Integer insertOrderStatus(WxOrderStatusDO wxOrderStatusDO) {
        int orderStatusRows = wxOrderStatusMapper.insert(wxOrderStatusDO);
        log.info("插入商户支付状态: {}条", orderStatusRows);
        return orderStatusRows;
    }

    @Override
    public String queryOrderStatusByOutTradeNo(String outTradeNo, String merid) {

        List<WxOrderInfoDO> wxOrderInfoDOList = wxOrderInfoMapper.selectList(new QueryWrapper<WxOrderInfoDO>()
                .eq("out_trade_no", outTradeNo)
                .eq("mchid", merid));
        if (wxOrderInfoDOList.size() <= 0) {
            log.error("未找到该商户的订单流水号的订单: outTradeNo: {}, mchid: {}", outTradeNo, merid);
            return "";
        }
        WxOrderInfoDO wxOrderInfoDO = wxOrderInfoDOList.get(0);
        List<WxOrderStatusDO> wxOrderStatusDOList = wxOrderStatusMapper.selectList(new QueryWrapper<WxOrderStatusDO>()
                .eq("order_id", wxOrderInfoDO.getId()));
        if (wxOrderInfoDOList.size() <= 0) {
            log.error("未找到该商户的该订单状态: outTradeNo: {}, mchid: {}", outTradeNo, merid);
        }
        WxOrderStatusDO wxOrderStatusDO = wxOrderStatusDOList.get(0);
        return wxOrderStatusDO.getOrderStatus();
    }

    @Override
    public Boolean changeOrderStatusByOutTradeNo(String outTradeNo, String merid, PayOrderType oldStatus, PayOrderType newStatus) {

        List<WxOrderInfoDO> wxOrderInfoDOList = wxOrderInfoMapper.selectList(new QueryWrapper<WxOrderInfoDO>()
                .eq("out_trade_no", outTradeNo)
                .eq("mchid", merid));
        if (wxOrderInfoDOList.size() <= 0) {
            return false;
        }

        WxOrderInfoDO wxOrderInfoDO = wxOrderInfoDOList.get(0);
        List<WxOrderStatusDO> wxOrderStatusDOList = wxOrderStatusMapper.selectList(new QueryWrapper<WxOrderStatusDO>()
                .eq("order_id", wxOrderInfoDO.getId())
                .eq("order_status", oldStatus.name()));
        if (wxOrderStatusDOList.size() <= 0) {
            return false;
        }
        WxOrderStatusDO wxOrderStatusDO = wxOrderStatusDOList.get(0);
        wxOrderStatusDO.setOrderStatus(newStatus.name());
        int updateRows = wxOrderStatusMapper.updateById(wxOrderStatusDO);
        if (updateRows > 0) {
            return true;
        }

        return false;
    }

    @Override
    public Boolean changeOrderStatusByOrderId(Long orderId, PayOrderType oldStatus, PayOrderType newStatus) {
        List<WxOrderStatusDO> wxOrderStatusDOList = wxOrderStatusMapper.selectList(new QueryWrapper<WxOrderStatusDO>()
                .eq("order_id", orderId)
                .eq("order_status", oldStatus.name()));
        if (wxOrderStatusDOList.size() <= 0) {
            return false;
        }
        WxOrderStatusDO wxOrderStatusDO = wxOrderStatusDOList.get(0);
        wxOrderStatusDO.setOrderStatus(newStatus.name());
        int updateRows = wxOrderStatusMapper.updateById(wxOrderStatusDO);
        if (updateRows > 0) {
            return true;
        }

        return false;
    }
}
